struct date {
     struct TreeNode*node;
 };
void tofind(struct TreeNode* root,struct TreeNode* p, struct TreeNode* q,int *flag){
     if(!root){
         return;
     }
     if(root==p){
         flag[0]=1;
     }
     if(root==q){
         flag[1]=1;
     }
     tofind(root->left,p,q,flag);
     tofind(root->right,p,q,flag);
 }

struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
    struct TreeNode*head=root;
    int *flag=malloc(sizeof(int)*2);
    struct date arr[200000];
    int top=0,ppre=1;
    arr[top].node=root;
    for(;arr[top].node!=p&&arr[top].node!=q;){
        flag[0]=0;
        flag[1]=0;
        tofind(arr[top].node,p,q,flag);
        if(flag[0]==1&&flag[1]==1){
            head=arr[top].node;
        }
        if(arr[top].node->left!=NULL){
            arr[ppre++].node=arr[top].node->left;
        }
        if(arr[top].node->right!=NULL){
            arr[ppre++].node=arr[top].node->right;
        }
        top++;
    }
    flag[0]=0;
    flag[1]=0;
    tofind(p,p,q,flag);
    if(flag[0]==1&&flag[1]==1){
        head=p;
    }
    flag[0]=0;
    flag[1]=0;
    tofind(q,p,q,flag);
    if(flag[0]==1&&flag[1]==1){
        head=q;
    }
    return head;
}